/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package koor.action.coursework;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import koor.action.Action;
import koor.action.ActionResult;
import koor.entity.CourseWork;
import koor.persistent.CourseWorkDAO;
import koor.service.ServiceLocator;

/**
 * The
 * <code>CourseWorkEditAction</code> class represents action, used to edit courseWork
 * information
 *
 * @author Admin
 */
public class CourseWorkEditAction implements Action {

    @Override
    public String getName() {
        return "courseWork.edit";
    }

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {
        CourseWorkDAO courseWorkDAO = ServiceLocator.getService(CourseWorkDAO.class);
        if (request.getParameter("id") != null) {
            try {
                Long id = Long.parseLong(request.getParameter("id"));
                CourseWork courseWork = courseWorkDAO.findOne(id);
                request.setAttribute("courseWork", courseWork);
                return ActionResult.forward("CourseWorkView/EditCourseWorkView.jsp");
            } catch (NumberFormatException e) {
                request.getSession().setAttribute("fail", "Неверный номер");
            } catch (RuntimeException e) {
                request.getSession().setAttribute("fail", "Курсовая не найдена");
            }
        }
        List<CourseWork> courseWorkList = courseWorkDAO.findAll();
        request.setAttribute("courseWorkList", courseWorkList);
        return ActionResult.forward("CourseWorkView/CourseWorkListView.jsp");
    }
}
